﻿Public Class XFormLogin 
    Dim ExisteEvento As Boolean

    Private Function ValidarForm() As Boolean
        If txtUser.Text.Trim = "" Then
            txtUser.Focus()
            Return False
        End If

        If txtPass.Text.Trim = "" Then
            txtPass.Focus()
            Return False
        End If

        If cboTipoAcceso.Text.Trim = "" Then
            cboTipoAcceso.Focus()
            Return False
        End If

        Return True
    End Function

    Private Sub XFormLogin_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Application.EnableVisualStyles()
    End Sub

    Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
        If Not ValidarForm() Then
            Exit Sub
        End If

        If cboTipoAcceso.Text = "Juez" Then
            Using TaEvento As New DsEventoTableAdapters.EventosTableAdapter
                Dim DtEvento As DataTable = TaEvento.GetDataByEstado(True)
                If DtEvento.Rows.Count = 1 Then
                    ExisteEvento = True
                Else
                    ExisteEvento = False
                End If
            End Using

            If ExisteEvento Then
                Using TaLogin As New DsSeguridadTableAdapters.JuecesTableAdapter
                    Dim DtLogin As DataTable = TaLogin.GetDataByLogin(txtUser.EditValue, SHA1(txtPass.EditValue))

                    If DtLogin.Rows.Count = 1 Then
                        With JuezActivo
                            .IDJuez = DtLogin(0)(0)
                            .Usuario = DtLogin(0)(1).ToString
                            .Nombre = DtLogin(0)(3).ToString
                            .Apellido = DtLogin(0)(4).ToString
                            .TipoAcceso = 1
                        End With
                        DialogResult = Windows.Forms.DialogResult.OK
                    Else
                        MsgBox("El usuario y/o contraseña no son validos", MsgBoxStyle.Critical, "Error")
                    End If
                End Using
            Else
                MsgBox("No puede iniciar sesión en este momento porque aun no se inicia ningún festival", MsgBoxStyle.Information, "No hay festival")
            End If
        Else
            Using TaLogin As New DsSeguridadTableAdapters.AdministradoresTableAdapter
                Dim DtLogin As DataTable = TaLogin.GetDataByLogin(txtUser.EditValue, SHA1(txtPass.EditValue))

                If DtLogin.Rows.Count = 1 Then
                    With JuezActivo
                        .IDJuez = DtLogin(0)(0)
                        .Usuario = DtLogin(0)(2).ToString
                        .Nombre = DtLogin(0)(1).ToString
                        .TipoAcceso = 2
                    End With
                    DialogResult = Windows.Forms.DialogResult.OK
                Else
                    MsgBox("El usuario y/o contraseña no son validos", MsgBoxStyle.Critical, "Error")
                End If
            End Using
        End If
    End Sub
End Class